/**
 * 响应拦截器
 * @param {*} vm 
 */
module.exports = (vm) => {
	uni.$u.http.interceptors.response.use((response) => {

		const data = response.data
		// console.log('%c [ 响应结果： ]', 'font-size:13px; background:pink; color:#bf2c9f;', data)
		// 自定义参数
		const custom = response.config?.custom
		if (custom.loading) uni.hideLoading();

		if (data.code !== 200) {
			// vm.$message.error(data.msg)
			exHandle(vm, data.code,data.msg)
			return Promise.reject(data)
		}

		return data === undefined ? {} : data
	}, (response) => {
		// 自定义参数
		const custom = response.config?.custom
		// if (custom.loading) uni.hideLoading();
		// （statusCode !== 200）
		exHandle(vm, response.statusCode)
		return Promise.reject(response)
	})


}
function exHandle(vm, code, errorMsg) {
	let msg = ""
	switch (code) {
		case 401:
			msg = errorMsg||"登录状态失效"
			setTimeout(() => {
				uni.reLaunch({
					url: '/pages/login/index'
				});
			}, 1000)
			vm.$store.commit("logout");
			break;
		case 403:
			msg = errorMsg||"没有权限"
			setTimeout(() => {
				uni.reLaunch({
					url: '/pages/login/index'
				});
			}, 1000)
			vm.$store.commit("logout");
			break;
		case 500:
			msg = errorMsg||"服务器错误"
			break;
		case 502:
			msg = errorMsg||"服务器异常"
			break;
		default:
			msg = errorMsg||"未知错误"
			break;
	}
	vm.$msg.error(msg)

}